package com.fingard.text;


import java.rmi.UnexpectedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

public class MFSHelper {
    static Pattern pattern = Pattern.compile("\\|");
    public static List<List<String>> retrieveMFSData(String text, int expectedFieldNum, int expectedRecordNum) throws UnexpectedException {
        List<List<String>> result = new ArrayList();
        for (String record : text.split("\\n")) {
            result.add(Arrays.asList(record.substring(0, record.length() - 1).split("\\|", -1)));
            if (result.get(result.size() - 1).size() != expectedFieldNum) {
                throw new UnexpectedException("MFS数据列数预期为" + expectedFieldNum + "，但是实际为" + result.get(result.size() - 1).size());
            }
        }
        if (result.size() != expectedRecordNum) {
            throw new UnexpectedException("MFS数据行数预期为" + expectedRecordNum + "，但是实际为" + result.size());
        }
        return result;
    }

    public static ArrayList<String[]> getTextArray(String p_textMFS, String p_splitChar, int p_fieldNum, int p_recordNum, StringBuilder p_sbLog) {
        ArrayList<String[]> retStrArr = new ArrayList<String[]>();
        try {
            int tmpLineCount = 0;//行数
            int tmpColumnCount = 0;//字段值计数
            int tmpColumnIndex = 0;//一个字段值的开始位置
            String[] tmpArrValue = new String[p_fieldNum];//存每行的值
            int tmpCmpLen = p_splitChar.length();
            for (int i = 0; i < p_textMFS.length(); i++) {
                if (i + tmpCmpLen <= p_textMFS.length()) {
                    String tmpEachCmp = p_textMFS.substring(i, i + tmpCmpLen);
                    if (tmpEachCmp.equals(p_splitChar)) {
                        String tmpEachColumnValue = p_textMFS.substring(tmpColumnIndex, i);//i - tmpColumnIndex
                        tmpColumnIndex = i + tmpCmpLen;
                        tmpArrValue[tmpColumnCount] = tmpEachColumnValue;
                        tmpColumnCount++;
                        if (tmpColumnCount == p_fieldNum) {
                            tmpLineCount++;
                            //计新的一行
                            tmpColumnCount = 0;
                            retStrArr.add(tmpArrValue);
                            tmpArrValue = new String[p_fieldNum];//新行
                        }

                        if (p_recordNum > 0 && tmpLineCount > p_recordNum && p_textMFS.length() > tmpColumnIndex) {
                            p_sbLog.append("实际内容行数"+tmpLineCount+"大于预期的行数：" + p_recordNum);
                            retStrArr.clear();
                            break;
                        }
                    }
                }

            }

            if (retStrArr.size() != p_recordNum && p_recordNum > 0) {
                p_sbLog.append("实际内容行数" + retStrArr.size() + "与预期内容行数" + p_recordNum + "不相等");
                retStrArr.clear();
            }
        } catch (Exception ex) {
            p_sbLog.append(ex.getMessage());
            retStrArr.clear();
        }
        return retStrArr;
    }

    /**根据行分隔符和列分隔符，提取出数据，第一行为列名，
     * 返回HashMap列表，HashMap的key为第一行的列表
     * 示例：
     * 交易日期|主机交易流水号|借方发生额|贷方发生额|0x0A
     * 20120814|010093040067|0.00|10000000.00
     * @param pText
     * @param pLineSp 行分隔符
     * @param pColumnSp 列分隔符
     * @throws Exception 
     * */
    public static ArrayList<HashMap<String,String>> pickupFirstLineIsColumn(String pText, String pLineSp, String pColumnSp) throws Exception{
    	ArrayList<HashMap<String,String>> retList = new ArrayList<HashMap<String,String>>();
    	String[] lines = pText.split(pLineSp);
    	HashMap<Integer,String> mapIdx = new HashMap<Integer,String>();
    	for(int i=0;i<lines.length;i++){
    		if(StringHelper.isNullOrEmpty(lines[i])){
    			break;
    		}
    		if(lines[i].trim().length()==0){
    			break;
    		}
    		String[] spValues = lines[i].split(pColumnSp);
    		if(i==0){//第一行，是列名
    			for(int j=0;j<spValues.length;j++){
    				mapIdx.put(j, spValues[j]);//每列的列名
    			}
    		}else if(spValues.length != mapIdx.size()){
    			throw new Exception("第"+(i+1)+"行，索引["+i+"]的数据“"+lines[i]+"”，列数"+spValues.length+"与预期"+mapIdx.size()+"不符");
    		}else{
    			HashMap<String,String> eachMap = new HashMap<String,String>();
    			for(int j=0;j<spValues.length;j++){
    				eachMap.put(mapIdx.get(j), spValues[j]);
    			}
    			retList.add(eachMap);
    		}
    	}
    	return retList;
    }
}
